<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class m_location extends CI_Model {

    var $id_loc;
    var $loc_name;
    var $loc_lati;
    var $loc_longi;
    var $loc_admin;

    public function __construct() {
        parent::__construct();
        //$this->loc_admin = $_COOKIE['c_idadmin'];
    }

    function set_id_loc($id_loc) {
        $this->id_loc = $id_loc;
    }

    function set_loc_name($loc_name) {
        $this->loc_name = $loc_name;
    }

    function set_loc_lati($loc_lati) {
        $this->loc_lati = $loc_lati;
    }

    function set_loc_longi($loc_longi) {
        $this->loc_longi = $loc_longi;
    }

    function get_location_byID($param) {
        $sql = "SELECT * FROM location l "
                . "JOIN admin a ON l.id_admin = a.id_admin "
                . "WHERE id_location = ?";
        $query = $this->db->query($sql, $param);
        $result = $query->row_array();

        return $result;
    }

    public function get_location_page($num, $offset){
        $sql = "SELECT * FROM location l "
                . "JOIN admin a ON l.id_admin = a.id_admin "
                . "ORDER BY location_name LIMIT $offset, $num";
        $query = $this->db->query($sql);
        $result = $query->result_array();

        return $result;
    }


    function insert_location($param) {
        $sql = "INSERT location (location_name, global_latitude, global_longitude, id_admin)"
                . "VALUES(?, ?, ?, ?)";
        $query = $this->db->query($sql, $param);
        
        if($query){
            return TRUE;
        }else{
            return FALSE;
        }
    }
    function get_all_location(){
        $sql = "SELECT * FROM location ORDER BY location_name";
        $query = $this->db->query($sql);

        if ($query) {
            $result = $query->result_array();
            $query->free_result();
            return $result;
        } else {
            return false;
        }
    }
    
    function get_by_location_name($param) {
        $sql = "SELECT * FROM location WHERE location_name LIKE ?";
        $query = $this->db->query($sql, $param);

        if($query->num_rows()>0){
            $result = $query->row_array();
            return $result;
        }else{
            return false;
        }
    }

    function update_location($param){
        $sql = "UPDATE location SET location_name = ?, global_longitude = ?, global_longitude = ? WHERE id_location = ?";
        $query = $this->db->query($sql, $param);

        if($query){
            return true;
        }else{
            return false;
        }
    }

    function delete_location($param){
        $sql = "DELETE FROM location WHERE id_location = ?";
        $query = $this->db->query($sql, $param);

        if($query){
            return true;
        }else{
            return false;
        }        
    }
}
